Return to start page
Core/Environment/Struct Jump.j
1 library AStructCoreEnvironmentJump requires optional ALibraryCoreDebugMisc, AStructCoreGeneralVector, ALibraryCoreMathsReal, ALibraryCoreMathsPoint, ALibraryCoreMathsUnit
2
3 /// @todo Should be a part of @struct AJump, vJass bug.
4 function interface AJumpAlightAction takes unit usedUnit returns nothing
5
6 struct AJump
7 //static start members
8 private static real m_refreshRate
9 private static string m_jumpAnimation
10 //static members
11 private static timer m_timer
12 private static AIntegerVector m_jumps
13 //dynamic members
14 private real m_speed
15 //start members
16 private unit m_unit
17 private real m_maxHeight
18 private real m_targetX
19 private real m_targetY
20 private AJumpAlightAction m_alightAction
21 //members
22 private integer m_index
23 private real m_startX
24 private real m_startY
25 private real m_distance
26 private real m_x
27
28 //! runtextmacro optional A_STRUCT_DEBUG("\"AJump\"")
29
30 //dynamic members
31
32 public method setSpeed takes real speed returns nothing
33 set this.m_speed = speed * thistype.m_refreshRate
34 endmethod
35
36 public method speed takes nothing returns real
37 return this.m_speed
38 endmethod
39
40 //start members
41
42 private method refreshPosition takes nothing returns boolean
43 set this.m_x = this.m_x + this.m_speed
44 //debug call this.print("Refresh. Distance is " + R2S(this.m_distance) + " refresh rate is " + R2S(thistype.m_refreshRate))
45 call SetUnitX(this.m_unit, GetPolarProjectionX(this.m_startX, GetUnitFacing(this.m_unit), this.m_x))
46 call SetUnitY(this.m_unit, GetPolarProjectionY(this.m_startY, GetUnitFacing(this.m_unit), this.m_x))
47 call SetUnitZ(this.m_unit, ParabolaZ(this.m_maxHeight, this.m_distance, this.m_x))
48 //debug call this.print("X is " + R2S(this.m_x))
49 //debug call this.print("New x is " + R2S(this.m_x))
50 return this.m_x >= this.m_distance
51 endmethod
52
53 public static method create takes unit usedUnit, real maxHeight, real targetX, real targetY, AJumpAlightAction alightAction returns thistype
54 local thistype this = thistype.allocate()
55 //dynamic members
56 call this.setSpeed(100.0)
57 //start members
58 set this.m_unit = usedUnit
59 set this.m_maxHeight = maxHeight
60 set this.m_targetX = targetX
61 set this.m_targetY = targetY
62 set this.m_alightAction = alightAction
63 //members
64 call thistype.m_jumps.pushBack(this)
65 set this.m_index = thistype.m_jumps.backIndex()
66 set this.m_startX = GetUnitX(usedUnit)
67 set this.m_startY = GetUnitY(usedUnit)
68 set this.m_distance = GetDistanceBetweenPoints(this.m_startX, this.m_startY, 0.0, targetX, targetY, 0)
69 set this.m_x = 0.0
70
71 call PauseUnit(usedUnit, true)
72 call SetUnitFacing(usedUnit, GetAngleBetweenPoints(this.m_startX, this.m_startY, targetX, targetY))
73
74 if (thistype.m_jumpAnimation != null) then
75 call SetUnitAnimation(usedUnit, thistype.m_jumpAnimation)
76 endif
77
78 return this
79 endmethod
80
81 public method onDestroy takes nothing returns nothing
82 call thistype.m_jumps.erase(this.m_index)
83 if (not IsUnitDeadBJ(this.m_unit) and this.m_unit != null) then //could be removed by user function
84 call PauseUnit(this.m_unit, false)
85
86 if (thistype.m_jumpAnimation != null) then
87 call ResetUnitAnimation(this.m_unit)
88 endif
89 endif
90 //start members
91 set this.m_unit = null
92 endmethod
93
94 /// @todo fast creation can cause crashes. behaviour is not change if vector members are erased in this method and not in destructor.
95 private static method timerFunction takes nothing returns nothing
96 local thistype jump
97 local integer i = thistype.m_jumps.backIndex()
98 loop
99 exitwhen (i < 0)
100 set jump = thistype.m_jumps[i]
101 if (jump.refreshPosition()) then
102 if (jump.m_alightAction != 0) then
103 call jump.m_alightAction.execute(jump.m_unit)
104 endif
105 call jump.destroy()
106 //do not increase i, jump was removed from vector
107 elseif (IsUnitDeadBJ(jump.m_unit)) then
108 call jump.destroy()
109 //do not increase i, jump was removed from vector
110 debug call thistype.staticPrint("Is Dead!")
111 endif
112 set i = i - 1
113 endloop
114 endmethod
115
116 public static method init takes real refreshRate, string jumpAnimation returns nothing
117 //static start members
118 set thistype.m_refreshRate = refreshRate
119 set thistype.m_jumpAnimation = jumpAnimation
120 //static members
121 set thistype.m_timer = CreateTimer()
122 call TimerStart(thistype.m_timer, thistype.m_refreshRate, true, function thistype.timerFunction)
123 set thistype.m_jumps = AIntegerVector.create()
124 endmethod
125
126 public static method cleanUp takes nothing returns nothing
127 call PauseTimer(thistype.m_timer)
128 call DestroyTimer(thistype.m_timer)
129 set thistype.m_timer = null
130 loop
131 exitwhen (thistype.m_jumps.empty())
132 call thistype(thistype.m_jumps.back()).destroy()
133 endloop
134 call thistype.m_jumps.destroy()
135 endmethod
136
137 public static method enable takes nothing returns nothing
138 call ResumeTimer(thistype.m_timer)
139 endmethod
140
141 public static method disable takes nothing returns nothing
142 call PauseTimer(thistype.m_timer)
143 endmethod
144 endstruct
145
146 endlibrary